<?php

namespace Admin\Controller;

use Think\Controller;
use Admin\Model\UserModel;
use Admin\Model\AuthModel;
/**
 * Description of CommonController
 *
 * @author wangnaixin
 * @todo 后台父类 处理 登录用户可以看到的菜单 初始化用户数据
 */


class AdminController extends Controller {
    //put your code here
    
    public $userId;
    public $userInfo;
    public $userAuths;
    //用户权限范围内的用户id列表
    public $userTeamMemberIds;
   

    function __construct() {
        parent::__construct();
        $this->checkUser();
        if($this->checkAuth(CONTROLLER_NAME,ACTION_NAME) === false){
            $this->error("非常抱歉，您没有权限访问该资源，请与系统管理员联系!");
        }
        $this->assign("controllerName",CONTROLLER_NAME);
        $this->assign("actionName",ACTION_NAME);
        $dayWeek = date('Y\年m\月d\日',time());
        $weeks = [
            '1'=>"一",
            '2'=>'二',
            '3'=>'三',
            '4'=>'四',
            '5'=>'五',
            '6'=>'六',
            '7'=>'七'
        ];
        $dayWeek .= " 星期".$weeks[date('N',time())];
        $this->assign("dayWeek",$dayWeek);
        $this->assign('sys_left_logo',session('sys_left_logo'));
        $this->assign('sysUserRole',$this->userInfo['role']);
        $this->setTopNav();
        $this->setSysPageTitle();
    }
    
    /**
     * @todo 判断用户是否登录 未登录跳转登录页面
     */
    private function checkUser(){
        $userInfo = session('userInfo');
        if(empty($userInfo)){
            redirect(U('Admin/Login/login')); 
        }
        $this->userId = session('userId');
        $this->userInfo = $userInfo;
        $this->userAuths = $userInfo['authList'];
        $this->userTeamMemberIds = $this->getTeamMemberByUser($this->userTeamIds);
        $menuList = $userInfo['menuList'];
        $this->assign("menuList",$menuList);
    }
    
    /**
     * @todo 判断用户是否有权限操作
     */
    private function checkAuth($c ,$a){
        if (in_array($c.'/'.$a,C('AUTH_WHITELIST'))) {
            return true;
        }
        if($this->userInfo['role']['super'] == 1){
            return true;
        }
        if(!in_array($a, $this->userAuths[$c])){
            return false;
        }else{
            return true;
        }
    }

    protected function setForm(){
        $formData = I('post.');
        $this->assign($formData);
    }
    
    protected function clearUserSession(){
        $user = new UserModel();
        $auth = new AuthModel();
        
        $data = $user->doLoginById($this->userId);
        //dump($data);
        if($data !== false){
            //检查用户权限
            if($data['role']['back_login']){
                $data['orgIds'] = $org->getOrgIds($data['orgs']);
                $isMerge = true;
                if ($data['role']['id'] == C('PIANQU_ADMIN_ROLE_ID')){
                    $isMerge = false;
                }
                //处理菜单
                if($data['role']['super'] == 1){
                    $data['menuList'] = menuInit($auth->where("isMenu=1")->select());
                }else{
                    $data['menuList'] = menuInit($data['auths']);
                }
                $data['authList'] = authInit($data['auths']);
                unset($data['auths']);
                session("userInfo",$data);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

   

    
   
    /**
     * 获取当前用户权限范围内的用户列表
     * @param int $teamId
     * @return array|int
     */
    protected function getTeamMemberByUser($teamId=0) {
        
        $memberId = array();
        $userRole = $this->userInfo['role'];
        if ($userRole['super'] == 0) {
            $userModel = new UserModel();
            switch ($userRole['id']) {
                case C('SPACE_ADMIN_ROLE_ID'):
                case C('TEAM_ADMIN_ROLE_ID'):
                    $memberId = $userModel->getUserByTeamId($teamId);
                    array_push($memberId,$this->userId);
                    break;
                case C('TEAM_MEMBER_ROLE_ID'):
                    $memberId = $this->userId;
                    break;
            }

        } else {
            //超级管理员返回0
            $memberId = 0;
        }
        return $memberId;
    }
    







    /**
     * 顶部导航设置
     */
    protected function setTopNav() {
        $topNav = array();
        if ($this->userInfo['role']['super'] == 1) {
            //系统管理员，显示更新缓存链接
            $topNav['showFresh'] = true;
        }
        $this->assign('topNav',$topNav);
    }

    /**
     * 系统后台标题设置
     * @return mixed|string
     */
    protected function setSysPageTitle() {
        $sysPageTitle = C('BUILDING_PAGE_TITLE');
        $this->assign('sysPageTitle',$sysPageTitle);
    }

}
